#include <msp430.h>
#include "pwm.h"
#include "main.h"

unsigned int pwm_out[PWM_OUTS];
unsigned int count = 0;


/*CCR1 Interrupt: This is the Pulse Width Modulation handler.*/
#ifdef __GNUC__
__attribute__((interrupt(TIMER0_A1_VECTOR)))
#else
#pragma vector = TIMER0_A0_VECTOR
	__interrupt
#endif
void TIMER0_A1_ISR(void)
{
	unsigned int index;
	unsigned int mask;
	index = 0x00;
	if(count == 0)
	{
		for(mask = 0x0001; index < PWM_OUTS-1; mask = mask << 0x01)
		{
			if(pwm_out[index++] > count)
			{
				P2OUT |= mask;
			}
		}
		if(pwm_out[index] > count)
		{
				P1OUT |= HEATER_PWM;
		}
	}
	else
	{
		for(mask = 0xFFFE; index < PWM_OUTS-1; mask |= 0x01)
		{
			if(pwm_out[index++] < count)
			{
				P2OUT &= mask;
			}
			mask  = mask << 0x01;
		}
		if(pwm_out[index] < count)
		{
			P1OUT &= ~(HEATER_PWM);
		}
	}
	count++;
	if(count > RESOLUTION) count = 0;
	
	//Mark the interrupt as serviced.
	TACCTL1 &= ~(TAIFG);

	//Exit LPM3
	__bic_SR_register_on_exit(LPM3_bits + GIE);
}
